// pages/new/new.js
//引用腾讯地图API
var QQMapWX = require('../../qqmap-wx-jssdk');
var qqmapsdk;
var location = "";
var config = require('../../config');
var app = getApp();
const db=wx.cloud.database()
const Order=db.collection('Order')
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    textcensus:0,
    max:200,
    start_date: "2020-01-01",
    activity_location: "",
    address: ""
  },

  tj:function(event){
    let value=event.detail.value
    let len=parseInt(value.length)
    if(len>200)return;
    this.setData({
      textcensus:len
    })

  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /*判断是第一次加载还是从position页面返回
        如果从position页面返回，会传递用户选择的地点*/
    console.log(options)
    if (options.address != null && options.address != '') {
      //设置变量 address 的值
      this.setData({
        address: options.address
      });
    } else {
      // 实例化API核心类
      qqmapsdk = new QQMapWX({
        //此key需要用户自己申请
        key: 'F22BZ-X25RK-TZ6JD-A5IAT-HQT7K-QTFSU'
      });
      var that = this;
      // 调用接口
      qqmapsdk.reverseGeocoder({
        success: function (res) {
          // console.log(res.result.address);
          that.setData({
            // address: res.result.address,
            activity_location: res.result.address
          });
          // console.log(that.data.address);
        },
        fail: function (res) {
          //console.log(res);
        },
        complete: function (res) {
          //console.log(res);
        }
      });
    }
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
 
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    console.log(getApp().data.activity_location);//从position跳转过来，可以
    // console.log(this.data.address);
    location = getApp().data.activity_location;
    if (location != "") {
      that.setData({
        activity_location: location
        // activity_location: this.data.address//这个没用，可能onshow最先获取不到onLoad值
      });
    }
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
 
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
 
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
 
  },
  /**
   * 选择日期并获取日期
   */
  bindStartDateChange: function (e) {
    this.setData({
      start_date: e.detail.value
    });
    console.log()
  },
 
   
  getLocation: function () {
  
    wx.navigateTo({
      url: '../position/position',
    });
  },
 
  formSubmit: function (event) {
    
    // console.log(types.l);
    if(event.detail.value.name!==''&&event.detail.value.Order_date!==''&&event.detail.value.activity_location!==''){
      db.collection('Order').add({
      data:{
        name:event.detail.value.name,
        activity_desc:event.detail.value.activity_desc,
        Order_date:event.detail.value.Order_date,
        activity_location:event.detail.value.activity_location
      }
    }).then(res=>{
      wx.showToast({
        image:'../../images/tjcg.png',
        title: '提交成功',
      })
    })
    }else{
      wx.showToast({
        title: '请填写完整信息',
        image:'../../images/title.png'
      })
    }
    

  }
})